/**
 * Created by panjun on 2018/11/2.
 */
import Header from '../../components/Header.vue';

export default {
	name: 'persons',
	components: {
		Header
	},
	mounted: function() {
		if (this.$route.params.companyId) {
			this.companyId = this.$route.params.companyId + "";
		}
		this.query(1);
		//保存下初始值
		this.defaultData = JSON.parse(JSON.stringify(this.addForm));
		this.personTypes = this.$getDictList("person_type");
		this.setCompanyList();


	},
	filters: {},
	methods: {
		setCompanyList: function() {
			let page = this;
			let userInfo = JSON.parse(localStorage.getItem("userInfo"));
			let companyId = userInfo.companyId.toString();
			this.companyName = userInfo.companyName;
			this.loading = true;
			this.axios.hPost("/api/company/query", {}, (res) => {
				page.loading = false;
				if (res.data) {
					page.companyList = page.getListByTree(res.data);
					if (!page.companyId) {
						page.companyId = companyId;
						page.query(1);
					}
				}
			});
		},

		getListByTree: function(companyTree) {
			let page = this;
			let list = [];
			companyTree.forEach(function(v) {
				list.push({
					id: v.id,
					value: v.text
				});
				if (v.children.length > 0) {
					let child = page.getListByTree(v.children);
					list = list.concat(child);
				}
			});
			return list;
		},
		handleUploadChange: function(file) {
			this.fileName = file.name;
		},
		beforeUpload: function(file) {
			this.loadingTxt = this.$t('message.fileUploading');
			this.loading = true;
			if (file.size > 52428800) {
				this.$message.error(this.$t('message.fileBig'));
				this.loading = false;
				this.loadingTxt = '';
				return false;
			}
		},
		//分页size变化
		handleSizeChange: function(val) {
			this.pageInfo.pageSize = val;
			this.query(1);
		},
		//翻页
		handleCurrentChange: function(val) {
			this.query(parseInt(val));
		},
		// 上传
		afterUpload: function(resp, file) {
			this.$message.success("上传成功")
			this.token = resp.data;
			this.fileName = this.fileName;
			this.imageUrl = URL.createObjectURL(file.raw);
			this.loading = false;
			this.loadingTxt = '';
		},
		errorUpload: function(err) {
			this.$message.error("上传失败")
			console.info(err);
		},

		// 分页查询
		query: function(pageNum) {
			this.loading = true;
			let params = {
				name: this.searchValue,
				companyId: this.companyId,
				type: this.type
			};
			this.axios.hPost('/api/persons?pageIndex=' + pageNum + '&pageSize=' + this.pageInfo.pageSize, params, (response) => {
				this.showImage = true;
				this.personData = response.list;
				this.pageInfo.totalNum = response.totalCount;
				this.pageInfo.currentPage = pageNum;
				this.loading = false;
			});
		},

		//删除图片
		delImage: function(id, faceId, personsId) {
			this.$confirm(this.$t('message.DDPicture'), this.$t('standard.delete'), {
				confirmButtonText: this.$t('standard.confirm'),
				cancelButtonText: this.$t('standard.cancel'),
				type: 'warning'
			}).then(() => {
				this.axios.hPost('/api/persons/deleteImage', {
					id: id,
					faceId: faceId,
					politicallyPersonsId: personsId,
					companyId: this.companyId
				}, (response) => {
					if (response.success) {
						this.queryImages();
						this.$message.success(response.msg);
					} else {
						this.$message.error(response.msg);
					}
				});
			})
		},

		// 显示增加窗口()
		showAddDialog: function() {
			this.formTitle = this.$t('persons.newCharacters');
			this.saving = false;
			if (this.$refs['addForm']) {
				this.$refs['addForm'].resetFields();
				this.addForm = Object.assign(this.addForm, this.defaultData);
			}
			this.token = "";
			this.addFormVisible = true;
			this.imageUrl = '';
		},

		// 显示修改窗口
		showModifyDialog: function(row) {
			this.formTitle = this.$t('persons.updateCharacters');
			this.saving = false;
			this.addFormVisible = true;
			if (this.$refs['addForm']) {
				this.$refs['addForm'].resetFields();
			}
			this.addForm.id = row.id;
			this.addForm.name = row.name;
			this.addForm.personageResume = row.personageResume;
			this.addForm.identityCard = row.identityCard;
			this.addForm.head_image = row.headImage;
			this.addForm.type = row.type;
			this.addForm.nation = row.nation;
			this.addForm.birthday = row.birthday;
			this.imageUrl = row.headImage;
		},

		//保存或修改人物
		save: function() {
			this.saving = true;
			let url = '/api/persons/save';
			this.$refs['addForm'].validate((valid) => {
				if (valid) {
					let params = {
						name: this.addForm.name,
						id: this.addForm.id,
						personageResume: this.addForm.personageResume,
						headImage: this.addForm.head_image,
						nation: this.addForm.nation,
						identityCard: this.addForm.identityCard,
						birthday: this.addForm.birthday,
						type: this.addForm.type,
						token: this.token,
						companyId: this.companyId
					};
					this.axios.hPost(url, params, (response) => {
						if (response.success) {
							this.$message.success(response.msg);
							this.addFormVisible = false;
							this.query(1);
						} else {
							this.$message.error(response.msg);
						}
						this.saving = false;
					});
				} else {
					this.saving = false;
					return false;
				}
			});
		},

		//删除人物
		deletePerson: function(id) {
			this.$confirm(this.$t('message.DDCharacter'), this.$t('standard.prompt'), {
				confirmButtonText: this.$t('standard.confirm'),
				cancelButtonText: this.$t('standard.cancel'),
				type: 'warning'
			}).then(() => {
				this.axios.hPost('/api/persons/delete', {
					id: id
				}, (response) => {
					if (response.success) {
						this.$message.success(response.msg);
						this.addFormVisible = false;
						this.query(1);
					} else {
						this.$message.error(response.msg);
					}
				});
			});
		},

		queryImages: function() {
			this.axios.hPost('/api/persons/findImages', {
				personId: this.personsId
			}, (response) => {
				this.imagesData = response;
				this.saving = false;
			});
		},
		showAddImages: function(row) {
			this.imageTitle = row.name;
			this.personsId = row.id;
			this.imageVisible = true;
			this.currentPerson = row;
			this.queryImages();
		},
		successUpload: function(resp, file, fileList) {
			this.token = resp.data;
			this.imageUrl = URL.createObjectURL(file.raw);
			var params = {
				politicallyPersonsId: this.personsId,
				token: this.token,
				companyId: this.companyId
			};
			this.axios.hPost('/api/persons/insertImage', params, (response) => {
				if (response.success) {
					this.imagesData = response.data;
					this.$message.success(response.msg);
					this.queryImages();
				} else {
					this.$message.error(response.msg);
				}
				this.saving = false;
			});
			this.loading = false;
			this.loadingTxt = '';
		},
		isShow: function(id) {
			document.getElementById(id).style.display = "block";
		},
		isHiden: function(id) {
			document.getElementById(id).style.display = "none";
		},

		/**
		 * 获取类型名称
		 * @param row
		 * @param column
		 * @param cellValue
		 * @param index
		 * @returns {string}
		 */
		getTypeName: function(row, column, cellValue, index) {
			let name = "";
			this.personTypes.forEach(function(e) {
				if (e.value === cellValue) {
					name = e.label;
					return;
				}
			});
			return name;
		},


		showInfoHtml: function(row) {
			this.formTitle = this.$t('persons.characterDetails');
			this.saving = false;
			this.infoVisible = true;
			/*if (this.$refs['addForm']) {
			 this.$refs['addForm'].resetFields();
			 }*/
			this.imageUrlInfo = '';
			this.infoStyle = "";
			this.infoForm.name = row.name;
			this.infoForm.personageResume = row.personageResume;
			this.infoForm.head_image = row.headImage;
			this.infoForm.type = row.type;
			this.infoForm.nation = row.nation;
			this.infoForm.birthday = row.birthday;
			this.imageUrlInfo = row.headImage;
			this.infoStyle = row.style;
		},
		showPic: function(path) {
			let a = document.createElement('a');
			a.setAttribute("href", path);
			a.setAttribute("target", "_blank")
			a.click();
		},
		//设置人物封面
		SetHeadImage: function(id, path) {
			this.$confirm(this.$t('message.haveCover'), this.$t('standard.confirm'), {
				confirmButtonText: this.$t('standard.confirm'),
				cancelButtonText: this.$t('standard.cancel'),
				type: 'warning'
			}).then(() => {
				this.currentPerson.headImage = path
				this.$message.success(this.$t('message.haveCoverSuccess'));
			});
		}
	},

	data: function() {
		return {

			//是否在加载标记
			loading: false,

			fileList: [],
			fileName: '',
			token: '',
			uuid: '',
			personData: [],
			noResult: false,
			showImage: false,
			loadingTxt: '',
			pageInfo: {
				currentPage: 1,
				pageSize: 10,
				totalNum: 0
			},

			showDel: false,
			searchValue: '',
			formTitle: '',
			addForm: {
				id: null,
				name: '',
				head_image: '',
				type: '',
				birthday: '',
				nation: '',
				personageResume: '',
				identityCard: ''
			},
			defaultData: {},
			addFormVisible: false,
			infoVisible: false,
			saving: false,
			imageUrl: '',
			imageVisible: false,
			imageTitle: '',
			imagesData: [],
			currentPerson: '',
			personsId: '',

			personTypes: [],
			type: '',
			infoStyle: '',

			addRules: {
				name: [{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						max: 50,
						message: this.$t('message.length50'),
						trigger: 'blur'
					},
					{
						pattern: /^([\u4e00-\u9FFFa-zA-Z0-9+\s]+)$/,
						message: this.$t('message.incorrectFormat'),
						trigger: 'blur'
					}
				],
				type: [{
					required: true,
					message: this.$t('message.fieldSelected'),
					trigger: 'blur'
				}],
				identityCard: [{
						max: 20,
						message: this.$t('message.length20'),
						trigger: 'blur'
					},
					{
						required: true,
						message: this.$t('message.required'),
						trigger: 'blur'
					},
					{
						pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/,
						message: this.$t('message.incorrectFormat'),
						trigger: 'blur'
					}
				],
				birthday: [{
					pattern: /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/,
					max: 20,
					message: this.$t('message.incorrectFormat'),
					trigger: 'blur'
				}],
				nation: [{
					max: 20,
					message: this.$t('message.length20'),
					trigger: 'blur'
				}]
			},
			companyId: "",
			companyName: "",
			//人物详情
			infoForm: {
				id: null,
				name: '',
				head_image: '',
				type: '',
				birthday: '',
				nation: '',
				personageResume: ''
			},
			imageUrlInfo: '',
			companyList: [],

			imageDelVisible: false,
			del_id: '',
		}
	}
}
